using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyCityCab.Core;
using System.Text;
using WebFramework.Utils;
using WebFramework;

public class AjaxHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        HttpContext cxt = HttpContext.Current;
        string result;
        Params queryString = new Params(cxt);
        ReturnValue<string> q = queryString.GetParam<string>("q");
        ReturnValue<int> limit = queryString.GetParam<int>("limit");
        ReturnValue<string> searchType = queryString.GetParam<string>("type");
        if (searchType.IsFalse || limit.IsFalse || q.IsFalse)
            return;
        ReturnValue<int> countryId;
        StringBuilder bldResult = new StringBuilder();
        switch (searchType.Value)
        {
            case "city":
                CityController cityController = new CityController();
                countryId = queryString.GetParam<int>("countryId");
                if (countryId.IsFalse)
                    break;
                CityCollection cityColl = cityController.SearchCities(q.Value, countryId.Value, limit.Value);
                foreach (City city in cityColl)
                {
                    bldResult.AppendLine(city.EnglishName);
                }
                break;
            case "region":
                RegionController regionController = new RegionController();
                countryId = queryString.GetParam<int>("countryId");
                if (countryId.IsFalse)
                    break;
                RegionCollection regionColl = regionController.SearchRegions(q.Value, countryId.Value, limit.Value);
                foreach (Region region in regionColl)
                {
                    bldResult.AppendLine(region.EnglishName);
                }
                break;
            case "Place Mark":
                break;
            default:
                break;
        }
        result = bldResult.ToString();
        cxt.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}